﻿namespace PicView.Core.FileHandling;

/// <summary>
/// Platform-agnostic manager for file associations that delegates to platform-specific implementations
/// </summary>
public static class FileAssociationManager
{
    private static IFileAssociationService? _service;
    
    /// <summary>
    /// Initializes the FileAssociationManager with the appropriate platform-specific implementation
    /// </summary>
    /// <param name="service">Platform-specific implementation of IFileAssociationService</param>
    public static void Initialize(IFileAssociationService? service)
    {
        _service = service ?? throw new ArgumentNullException(nameof(service));
    }

    /// <summary>
    /// Associates a single file extension with the application
    /// </summary>
    /// <param name="fileExtension">The file extension to associate</param>
    /// <param name="description">Optional custom description for the file type</param>
    /// <returns>True if successful, false otherwise</returns>
    public static async Task<bool> AssociateFile(string fileExtension, string? description = null)
    {
        EnsureInitialized();
        // Use provided description or generate a default one
        var fileDescription = description ?? $"{fileExtension.TrimStart('.')} Image File";
        return await _service.RegisterFileAssociation(fileExtension, fileDescription);
    }
    
    /// <summary>
    /// Unassociates a single file extension
    /// </summary>
    public static async Task<bool> UnassociateFile(string fileExtension)
    {
        var isAssociated = await IsFileAssociated(fileExtension);
        if (isAssociated)
        {
            return await _service.UnregisterFileAssociation(fileExtension);
        }
        
        return false;
    }
    
    /// <summary>
    /// Checks if a file extension is associated with the application
    /// </summary>
    public static async Task<bool> IsFileAssociated(string fileExtension)
    {
        EnsureInitialized();
        return await _service.IsFileAssociated(fileExtension);
    }
    
    private static void EnsureInitialized()
    {
        if (_service == null)
        {
            throw new InvalidOperationException(
                "FileAssociationManager has not been initialized. Call Initialize() with an appropriate implementation before using this class.");
        }
    }
}